package org.esgi.user.model.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.esgi.user.model.ConnectionMysql;
import org.esgi.user.model.User;
import org.esgi.user.model.collection.UserCollection;
import org.esgi.user.model.interfaces.IDao;

public class UserDao2 extends ConnectionMysql implements IDao{

	private ConnectionMysql _connection = null;
	
	public UserDao2() {
		super();
//		_connection = ConnectionMysql.getInstance();
	}
	

	/**
	 * Return the associated User for the given id.
	 * 
	 * @param userId
	 * @return
	 * @throws SQLException
	 */

	public ArrayList<ArrayList<Object>> selectAll() throws SQLException {
		if (null == selectById)
			selectById = this.getConnection().prepareStatement("SELECT * FROM user ");
//		selectById.setInt(1, 1);
		ResultSet result = selectById.executeQuery();
		ArrayList<ArrayList<Object>> users = new ArrayList<ArrayList<Object>>();
		
//		users = null;
		int i = 0;
		
		while (result.next()) {
			ArrayList<Object> champ = new ArrayList<Object>();
			//Add column
			champ.add(0, result.getInt("user_id"));
			champ.add(1, result.getString("login"));
			champ.add(2, result.getString("password"));
			champ.add(3, result.getString("fullname"));
			champ.add(4, result.getString("email"));
			champ.add(5, result.getDate("created_at"));
			champ.add(6, result.getInt("newsletter"));
			champ.add(6, result.getInt("status"));
			champ.add(7, result.getInt("job"));
			
			//Add row
			users.add(i, champ);
			i++;
		}
		return users;
	}

	@Override
	public boolean insert(UserCollection user) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public UserCollection select(Integer userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserCollection selectByColumn(String column, Object value) throws SQLException {
		System.out.println(value);
		if (null == selectById)
			selectById = this.getConnection()
			.prepareStatement("SELECT * FROM user WHERE "+column+" LIKE ?");
//		selectById.setString(1, column);
		selectById.setObject(1, value);
		System.out.println(selectById);
		ResultSet result = selectById.executeQuery();
		if (result.next()) {
			UserCollection u = new UserCollection();
			u.setCreated_at(result.getDate("created_at"));
			u.setLogin(result.getString("login"));
			u.setPassword(result.getString("password"));
			u.setStatus(result.getInt("status"));
			return u;
		}
		return null;
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}
}